﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Runtime.Serialization;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using WmsApp.Exceptions;
using WmsApp.Security;
using WmsApp.Service;
using WmsApp.WmsService;

namespace WmsApp.Window
{
    public partial class FormUserManage : BaseForm
    {
        public FormUserManage()
        {
            InitializeComponent();
            Lookup.LookupBind.BindingLookUpEditIsAlive(this.gridViewMain,this.gridColumn5);
            gridViewMain.CustomRowCellEdit += GridViewMainCustomRowCellEdit;
            CmdQuery_Click(null, null);
        }

        void GridViewMainCustomRowCellEdit(object sender, DevExpress.XtraGrid.Views.Grid.CustomRowCellEditEventArgs e)
        {
            if (e.Column == this.gridColumn1)
            {
                var user = this.gridViewMain.GetRow(e.RowHandle) as SystemUser;
                if (user != null)
                {
                    if (user.WcfEntityState != EntityState.Added)
                    {
                        e.RepositoryItem = base.RepTextEditReadOnly;
                    }
                }
            }
        }

        public override string HeadText
        {
            get { return "用户管理"; }
        }

        private void CmdQuery_Click(object sender, EventArgs e)
        {
            var client = EntityServiceFactory.GetEntityService<SystemUser,SystemUserService>();
            var list = client.GetEntityList();
            this.gridControlMain.DataSource = list;
        }

        private void CmdNew_Click(object sender, EventArgs e)
        {
            var user = SystemUser.CreateSystemUser("请更改");
            if(this.gridControlMain.DataSource == null)
            {
                this.gridControlMain.DataSource = new List<SystemUser>();
            }
            ( (List<SystemUser>) this.gridViewMain.DataSource).Add(user);
            this.gridControlMain.RefreshDataSource();

            for(int idx = 0;idx<  ( (List<SystemUser>) this.gridViewMain.DataSource).Count;idx++)
            {
                var u =(SystemUser) this.gridViewMain.GetRow(idx);
                
                if( u== user)
                {
                    this.gridViewMain.FocusedRowHandle = idx;
                }
            }
        }

        private void CmdSave_Click(object sender, EventArgs e)
        {
            try
            {
                EntityServiceFactory.GetEntityService<SystemUser, SystemUserService>().
                SaveEntityList( EntityControl.EntityFilterByState.GetNeedSaveEntity(this.gridViewMain.DataSource as List<SystemUser>));
                XtraMessageBox.Show("保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            catch (Exception ex)
            {
                ExceptionHandler.ShowException(ex);
            }
        }

        private void CmdSetPass_Click(object sender, EventArgs e)
        {
            object obj = this.gridViewMain.GetRow(this.gridViewMain.FocusedRowHandle);
            if (obj != null)
            {
                string userID = ((SystemUser) obj).USERID;
                if (((SystemUser)obj).WcfEntityState == EntityState.Added)
                {
                    XtraMessageBox.Show("请先保存再设置密码！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                try
                {
                    PasswordService.SetPassword(userID);
                }
                catch (Exception ex)
                {
                    ExceptionHandler.ShowException(ex);
                }
            }
        }
    }
}